package com.cqc.googleplay.fragment.home;

import java.util.ArrayList;
import java.util.List;

import com.cqc.googleplay.R;
import com.cqc.googleplay.adapter.HomeFragAdapter;
import com.cqc.googleplay.bean.AppInfoBean;
import com.cqc.googleplay.bean.HomeFragBean;
import com.cqc.googleplay.http.HttpHelper;
import com.cqc.googleplay.http.NetUrl;
import com.cqc.googleplay.utils.CommonUtil;
import com.cqc.googleplay.utils.GsonUtil;
import com.cqc.googleplay.view.ContentPager;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class AppFrag extends Fragment {
	
	private View view;
	private PullToRefreshListView ptr;
	private List<AppInfoBean> appsList = new ArrayList<AppInfoBean>();
	private HomeFragAdapter adapter;
	private ListView listView;
	private static final int WHAT1 = 1;//下拉
	private static final int WHAT2 = 2;//上拉

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentPager = new ContentPager(getActivity()) {

			@Override
			public Object requestData() {
				return getData();
			}

			@Override
			public View createSuccessView() {
				return getSuccessView();
			}
		};
		return contentPager;
	}

	protected View getSuccessView() {
		view = View.inflate(getActivity(), R.layout.frag_home, null);
		ptr = (PullToRefreshListView) view.findViewById(R.id.pull_refresh_list);
		ptr.setMode(Mode.BOTH);
		ptr.setOnRefreshListener(new OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				if (refreshView.getCurrentMode() == Mode.PULL_FROM_START) {//下拉刷新
					handler.sendEmptyMessageDelayed(WHAT1, 1500);
				} else {//上拉加载更多
//					handler.sendEmptyMessageDelayed(WHAT2, 1500);
					contentPager.loadDataAndRefresh();
				}
			}
			
		});
		listView = ptr.getRefreshableView();
		adapter = new HomeFragAdapter(appsList);
		listView.setAdapter(adapter);
		return view;
	}

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.what == WHAT1) {
				if (ptr.isRefreshing()) {
					ptr.onRefreshComplete();
				}
			} else if (msg.what == WHAT2) {
				adapter.notifyDataSetChanged();
				if (ptr.isRefreshing()) {
					ptr.onRefreshComplete();
				}
			}
			
		};
	};
	private ContentPager contentPager;
	protected Object getData() {
		String json = HttpHelper.get(NetUrl.APP +appsList.size());
		List<AppInfoBean> list = (List<AppInfoBean>) GsonUtil.jsonToList(json, new TypeToken<List<AppInfoBean>>() {}.getType());
		addAndRefreshData(list);
		return list;
	}

	private void addAndRefreshData(List<AppInfoBean> list) {
		appsList.addAll(list);
		
		CommonUtil.runOnUIThread(new Runnable() {
			
			@Override
			public void run() {
				adapter.notifyDataSetChanged();
				if (ptr.isRefreshing()) {
					ptr.onRefreshComplete();
				}
			}
		});
	}
}
